package com.huiyujinrong.qiangdanbao.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.bean.FragmentGrabSingleBean;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FragmentGrabSingleAdapter extends BaseAdapter {

    private Context context;
    private List<FragmentGrabSingleBean.list> list;

    public FragmentGrabSingleAdapter(Context context, List<FragmentGrabSingleBean.list> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_grabsingle, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.money.setText(list.get(position).getApply_amt() == null ? "" : list.get(position).getApply_amt()); //借款金额
        holder.month.setText(list.get(position).getLoan_term() == null ? "" : list.get(position).getLoan_term());  //还款时间
        String name = list.get(position).getTrue_name();
        int sex = list.get(position).getSex();
        if (name != null && name.length() > 1) {
            String tempName = String.format("%s%s", name.substring(0, 1), sex == 1 ? "先生" : "女士");
            holder.name.setText(tempName); //姓名
        } else {
            holder.name.setText(name == null ? "" : name); //姓名
        }


        DataUtils.getInstance();
        long time = list.get(position).getCreate_time() / 1000;
        holder.time.setText(DataUtils.getTimeElapse(time));  //多少时间前

        holder.city.setText(DataUtils.getStringData(list.get(position).getCity_name())); //城市


        switch (list.get(position).getLoan_type()) {  //借款类型 1：工薪族  2：企业主
            case 1:
                holder.type.setText("工薪族");
                holder.yueMoney.setText("月收入 " + list.get(position).getMonth_income() + "元");   //月收入
                break;
            case 2:
                holder.type.setText("企业主");
                holder.yueMoney.setText("对私流水 " + list.get(position).getBank_dsls() + "万元");   //月收入
                break;
        }

        String birthDath = list.get(position).getBirth_date();
        if (!TextUtils.isEmpty(birthDath)) {
            int oldYear = resolveDateString2Year(list.get(position).getBirth_date() == null ? "" : list.get(position).getBirth_date());
            int newYear = getCurrentYear();
            holder.age.setText(String.valueOf(newYear - oldYear) + "岁");  //年龄
        } else {
            holder.age.setText("0岁");  //年龄
        }


        //教育情况   1:初中以下2：初中3：高中/中专/技校4：专科5：本科6：硕士7：博士及以上

        //房产状况
        switch (list.get(position).getHouse_state()) {
            case 1:
                holder.isFang.setText("自置无按揭");
                break;
            case 2:
                holder.isFang.setText("自置有按揭");
                break;
            case 3:
                holder.isFang.setText("亲属楼宇");
                break;
            case 4:
                holder.isFang.setText("集体宿舍");
                break;
            case 5:
                holder.isFang.setText("租房");
                break;
            case 6:
                holder.isFang.setText("自建房");
                break;
            case 7:
                holder.isFang.setText("共有");
                break;
            case 8:
                holder.isFang.setText("有商铺/商住两用");
                break;
            case 9:
                holder.isFang.setText("其他");
                break;
        }
        //车状况
        switch (list.get(position).getCar_state()) {
            case 1:
                holder.isCar.setText("无车产");
                break;
            case 2:
                holder.isCar.setText("有车产无抵押");
                break;
            case 3:
                holder.isCar.setText("有车产无抵押");
                break;
        }


        //抢单状态：1:立即抢单2：被抢
        switch (list.get(position).getLoan_status()) {
            case 1:
                holder.qiangDan.setVisibility(View.VISIBLE);  // 立即抢单
                holder.beiQiang.setVisibility(View.GONE);
                break;
            case 2:
                holder.beiQiang.setVisibility(View.VISIBLE);  // 已被抢单
                holder.qiangDan.setVisibility(View.GONE);
                break;
        }
        return convertView;
    }


    private int resolveDateString2Year(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date oldDate = null;
        try {
            oldDate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        return calendar.get(Calendar.YEAR);
    }

    private int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        return calendar.get(Calendar.YEAR);
    }

    class ViewHolder {
        @BindView(R.id.gs_money)
        TextView money;
        @BindView(R.id.gs_month)
        TextView month;
        @BindView(R.id.gs_name)
        TextView name;
        @BindView(R.id.gs_time)
        TextView time;
        @BindView(R.id.gs_city)
        TextView city;
        @BindView(R.id.gs_type)
        TextView type;
        @BindView(R.id.gs_age)
        TextView age;
        @BindView(R.id.gs_xueli)
        TextView xueli;
        @BindView(R.id.gs_yueInputMoney)
        TextView yueMoney;
        @BindView(R.id.gs_isFang)
        TextView isFang;
        @BindView(R.id.gs_isCar)
        TextView isCar;

        @BindView(R.id.gs_beiQiang)
        ImageView beiQiang;
        @BindView(R.id.gs_qiangDan)
        ImageView qiangDan;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
